package com.ibeeking.gmc.global.g.rest.service.impl;

import com.ibeeking.gmc.global.g.rest.service.*;
import com.ibeeking.gmc.global.service.common.param.*;
import com.ibeeking.gmc.global.service.common.vo.*;
import com.ibeeking.found.common.utils.DataUtils;
import com.ibeeking.found.common.utils.UserUtils;
import com.ibeeking.gmc.global.g.rest.service.*;
import com.ibeeking.gmc.global.service.common.constants.enums.TenantAuditStatusEnum;
import com.ibeeking.nematos.core.utils.SpringBeanUtil;
import com.ibeeking.nematos.utils.data.ParamCheckUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @ClassName: TenantCommerceCertServiceImpl
 * @Author ibeeking
 * @Description:
 * @Date: 2021/11/26 14:44
 * @Version: 1.0
 */
@Service
public class TenantIdentificationServiceServiceImpl implements ITenantIdentificationService {

    @Resource
    private com.ibeeking.gmc.global.g.rest.service.ITenantEnterpriseInfoService ITenantEnterpriseInfoService;
    @Resource
    private com.ibeeking.gmc.global.g.rest.service.ITenantLegalInfoService ITenantLegalInfoService;
    @Resource
    private IAgentInfoService agentInfoService;
    @Resource
    private ITenantBankService iTenantBankService;
    @Resource
    private ITenantTrademarkAuthInfoService tenantTrademarkAuthInfoService;
    @Resource
    private ITenantBrandAuthInfoService tenantBrandAuthInfoService;

    @Override
    public TenantAuditStatusVO auditStatusView() {
        TenantAuditStatusVO tenantAuditStatusVO = new TenantAuditStatusVO();
        //主体信息审核状态
        TenantEnterpriseInfoVO tenantEnterpriseInfoVO = getBean().enterpriseInfoView();
        tenantAuditStatusVO.setEnterpriseAuditStatus(tenantEnterpriseInfoVO == null || tenantEnterpriseInfoVO.getAuditStatus() == null ? TenantAuditStatusEnum.SUBMIT_NO.getValue() : tenantEnterpriseInfoVO.getAuditStatus());
        //银行账号审核状态
        TenantBankVO tenantBankVO = getBean().bankView();
        tenantAuditStatusVO.setBankAuditStatus(tenantBankVO == null || tenantBankVO.getAuditStatus() == null ? TenantAuditStatusEnum.SUBMIT_NO.getValue() : tenantBankVO.getAuditStatus());
        //商标审核状态
        TenantTrademarkAuthInfoVO tenantTrademarkAuthInfoVO = getBean().trademarkAuthView();
        tenantAuditStatusVO.setTrademarkAuthAuditStatus(tenantTrademarkAuthInfoVO == null || tenantTrademarkAuthInfoVO.getAuditStatus() == null ? TenantAuditStatusEnum.SUBMIT_NO.getValue() : tenantTrademarkAuthInfoVO.getAuditStatus());
        //品牌审核状态
        TenantBrandAuthInfoVO tenantBrandAuthInfoVO = getBean().brandAuthView();
        tenantAuditStatusVO.setBrandAuthAuditStatus(tenantBrandAuthInfoVO == null || tenantBrandAuthInfoVO.getAuditStatus() == null ? TenantAuditStatusEnum.SUBMIT_NO.getValue() : tenantBrandAuthInfoVO.getAuditStatus());
        return tenantAuditStatusVO;
    }

    @Override
    public Boolean auditStatusChange(TenantAuditStatusParam param) {
        Integer tenantId = Integer.valueOf(UserUtils.getTenantId());
        //主体信息审核状态

        if (param.getEnterpriseAuditStatus() != null) {
            ITenantEnterpriseInfoService.changeStatus(tenantId, param.getEnterpriseAuditStatus());
        }
        //银行账号审核状态
        if (param.getBankAuditStatus() != null) {
            iTenantBankService.changeStatus(tenantId, param.getBankAuditStatus());
        }
        //商标审核状态
        if (param.getTrademarkAuthAuditStatus() != null) {
            tenantTrademarkAuthInfoService.changeStatus(tenantId, param.getTrademarkAuthAuditStatus());
        }
        //品牌审核状态
        if (param.getBrandAuthAuditStatus() != null) {
            tenantBrandAuthInfoService.changeStatus(tenantId, param.getBrandAuthAuditStatus());
        }
        return true;
    }

    @Override
    public TenantEnterpriseInfoVO enterpriseInfoView() {
        return ITenantEnterpriseInfoService.viewByTenantId(Integer.valueOf(UserUtils.getTenantId()));
    }

    @Override
    public Boolean enterpriseInfoSave(TenantEnterpriseInfoParam param) {
        param.setTenantId(Integer.valueOf(UserUtils.getTenantId()));

        return ITenantEnterpriseInfoService.save(param);
    }

    @Override
    public LegalAndAgentInfoVO legalAndAgentInfoView() {
        LegalAndAgentInfoVO legalAndAgentInfoVO = new LegalAndAgentInfoVO();
        Integer tenantId = Integer.valueOf(UserUtils.getTenantId());
        TenantLegalInfoVO tenantLegalInfoVO = ITenantLegalInfoService.viewByTenantId(tenantId);
        legalAndAgentInfoVO.setLegalInfo(ITenantLegalInfoService.viewByTenantId(tenantId));
        if (tenantLegalInfoVO != null && tenantLegalInfoVO.getOperator() == 2) {
            legalAndAgentInfoVO.setAgentInfo(agentInfoService.viewByTenantId(tenantId));
        }
        return legalAndAgentInfoVO;
    }

    @Override
    public Boolean legalAndAgentInfoSave(TenantLegalAndAgentInfoParam param) {
        TenantLegalInfoParam tenantLegalInfoParam = param.getLegalInfo();
        DataUtils.trueThrowException(tenantLegalInfoParam == null, "请填写法人信息");
        Integer operator = tenantLegalInfoParam.getOperator();
        if (operator == null) {
            operator = 1;
            tenantLegalInfoParam.setOperator(operator);
        }
        TenantAgentInfoParam tenantAgentInfoParam = param.getAgentInfo();
        if (operator == 2) {
            DataUtils.trueThrowException(tenantAgentInfoParam == null, "请填写代办人信息");
            ParamCheckUtils.check(tenantAgentInfoParam);
        }
        Integer tenantId = Integer.valueOf(UserUtils.getTenantId());
        tenantLegalInfoParam.setTenantId(tenantId);
        tenantAgentInfoParam.setTenantId(tenantId);
        ITenantLegalInfoService.save(tenantLegalInfoParam);
        if (operator == 2) {
            agentInfoService.save(tenantAgentInfoParam);
        } else {
            agentInfoService.delByTenantId(tenantId);
        }
        return true;
    }

    @Override
    public TenantBankVO bankView() {
        return iTenantBankService.queryByTenantId(Integer.valueOf(UserUtils.getTenantId()));
    }

    @Override
    public Boolean bankSave(TenantBankParam param) {
        param.setTenantId(Integer.valueOf(UserUtils.getTenantId()));
        TenantBankVO tenantBankVO = iTenantBankService.queryByTenantId(param.getTenantId());
        if (tenantBankVO == null) {
            iTenantBankService.add(param);
        } else {
            tenantBankVO.setId(tenantBankVO.getId());
            iTenantBankService.modify(param);
        }
        return true;
    }

    @Override
    public TenantTrademarkAuthInfoVO trademarkAuthView() {
        return tenantTrademarkAuthInfoService.queryByTenantId(Integer.valueOf(UserUtils.getTenantId()));
    }

    @Override
    public Boolean trademarkAuthSave(TenantTrademarkAuthInfoParam param) {
        param.setTenantId(Integer.valueOf(UserUtils.getTenantId()));
        return tenantTrademarkAuthInfoService.save(param);
    }

    @Override
    public TenantBrandAuthInfoVO brandAuthView() {
        return tenantBrandAuthInfoService.queryByTenantId(Integer.valueOf(UserUtils.getTenantId()));
    }

    @Override
    public Boolean brandAuthSave(TenantBrandAuthInfoParam param) {
        param.setTenantId(Integer.valueOf(UserUtils.getTenantId()));
        return tenantBrandAuthInfoService.save(param);
    }

    private static TenantIdentificationServiceServiceImpl getBean() {
        return SpringBeanUtil.getBean(TenantIdentificationServiceServiceImpl.class);
    }


}
